package com.metacube.metaconnect.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.metacube.metaconnect.Service.UserInformationService;
import com.metacube.metaconnect.model.ForgotPasswordForm;

public class ForgotPasswordAction extends Action {

	/*
	 * SEND THE PASSWORD OF USER TO HIS/HER EMAIL
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		System.out.println("klai");
		ForgotPasswordForm forgotform = (ForgotPasswordForm) form;

		String email = forgotform.getEmail();
		try {
			String password = new UserInformationService()
					.getActiveUserPassword(email);

			if (password == null) {
				request.setAttribute("forgotPasswordError",
						"You Are Not A User Of MetaConnect..Please Join By Signing Up");
			} else {
				new SendEmail().sendMail(email, "your MetaConnect Password",
						"Your MetaConnect ID Password is " + password);
				request.setAttribute("forgotPasswordError",
						"Password Successfully sent to " + email);
			}
		} catch (Exception e) {
			request.setAttribute("forgotPasswordError",
					"Error: Password Could Not Be Located!! Please Try Letter");
		}

		return mapping.findForward("success");
	}
}
